package com.starinside.wp.repository;

import com.starinside.wp.domain.CheckRecord;
import com.starinside.wp.domain.record.LiveRecord;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * *******************************
 * Created by Tim.
 * form com.matt
 * on 2017/7/25
 * *******************************
 */
@Repository
public interface LiveRecordRepository extends JpaRepository<LiveRecord, String> {

    @Query(value = " select * from live_record l " +
            "left join rel_children_live_record r on r.`liveRecordId` = l.`id` " +
            "where r.childrenId in ?1 and l.deleted = 0 order by l.`created` desc",
            countQuery = " select count(1) from live_record l " +
                    "left join rel_children_live_record r on r.`liveRecordId` = l.`id` " +
                    "where r.childrenId in ?1 and l.deleted = 0",
            nativeQuery = true)
    Page<LiveRecord> findLiveRecordByChildrenIds(String[] childrenIds, Pageable pageable);
//    {
//
//        if (childrenIds == null || childrenIds.length == 0){
//            return null;
//        }
//
//        String childrenIdsStr = "'" + StringUtils.join(childrenIds, "\',\'") + "'";
//
//        String sql = "SELECT title,content,category,resourceUrl,thumbUrl,created,mediaType,operatorId, operatorName,headImgUrl " +
//                "FROM live_record where  id in (" +
//                "SELECT liveRecordId from rel_children_live_record where childrenId in (%s)" +
//                ") order by created desc limit  %d,%d";
//
//        sql =  String.format(sql, childrenIdsStr, pageNum * pageSize, pageSize);
//
//        List<LiveRecord> items = sqlObjectList(sql, LiveRecord.class);
//
//        return items;
//    }

}
